package cn.thinkluck.pattern.proxy.dynamic.cglib;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.Enhancer;

/**
 * cglib 动态代理
 * @Author liutao
 * @DateTime 2024/7/20 下午4:12
 */
@Slf4j
public class Main {
    public static void main(String[] args) {
        //通过Enhancer创建代理类
        Enhancer enhancer = new Enhancer();
        //设置父类
        enhancer.setSuperclass(DatabaseQuery.class);
        //设置回调方法
        enhancer.setCallback(new CacheMethodInterceptor(new DatabaseQuery()));
        //创建代理类
        DatabaseQuery proxy = (DatabaseQuery) enhancer.create();
        String value1 = proxy.query("key1");
        log.info(value1);
        String value2 = proxy.query("key1");
        log.info(value2);
    }
}
